package com.joinway.framework.admin.core.service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.joinway.framework.admin.bean.domain.AdminUser;
import com.joinway.framework.admin.core.constant.AdminPageKeyConstants;
import com.joinway.framework.admin.core.utils.SessionHelper;
import com.joinway.framework.bean.exception.SingleSignOnException;
import com.joinway.framework.bean.sso.SSOContext;
import com.joinway.framework.core.repository.FrameworkRepository;
import com.joinway.framework.core.repository.SystemConfigRepository;
import com.joinway.framework.core.utils.FrameworkHelper;
import com.joinway.framework.support.sso.ISingleSignOnService;

@Service
public class AdminWebService extends AbstractAdminWebService {

	private final static Logger log = LoggerFactory.getLogger(AdminWebService.class);
	
//	@Autowired protected TableRepository repository;
	
	@Autowired protected SystemConfigRepository systemRepository;
	
//	@Autowired protected AdminQueryService qservice;
	
	protected ISingleSignOnService<SSOContext> ssoService;
	
	protected String indexPage = "index";
	
	@SuppressWarnings("unchecked")
	@Override
	public ModelAndView ssoLogin(String token, HttpServletRequest request) throws Exception {
		ModelAndView mav = new ModelAndView();
		
		ssoService = FrameworkRepository.getBean(ISingleSignOnService.class);
		
		SSOContext context = ssoService.getSSOContext(token);
		
		if(context == null){
			throw new SingleSignOnException();
		}
		
		AdminUser user = repository.findById(AdminUser.class, context.getUserId());
		if(user == null){
			mav.setViewName(context.getRedirectUrlOnFail());
		}else{
			mav.setViewName(context.getRedirectUrlOnSuccess());
			
			super.postLogin(user, request);
		}
		
		return mav;
	}

	@Override
	public ModelAndView index() {
		ModelAndView mv = new ModelAndView(getIndexPage());
		
		try{
			String siteName = systemRepository.getStringValue(SystemConfigRepository.AdminSiteName);
			HttpSession session = FrameworkHelper.getHttpServletRequest().getSession();
			session.setAttribute("adminSiteName", siteName);
		}catch(Exception e){
			log.error("failed to get admin site name", e);
		}
		
		return mv;
	}
	
	@Override
	public ModelAndView logout() throws Exception {
		String suffix = systemRepository.getStringValue(SystemConfigRepository.PageRequestSuffix);
		
		return new ModelAndView("redirect:" + getIndexPage() + suffix);
	}
	
	protected ModelAndView processProfile(ModelAndView mv) throws Exception {
		ModelAndView mav = mv;
		
		int userId = SessionHelper.getUserContext().getUser().getId();
		
		AdminUser user = repository.findById(AdminUser.class, userId);
		
		mav.addObject(AdminPageKeyConstants.USER_NAME, user.getName());
		mav.addObject(AdminPageKeyConstants.EMAIL, user.getEmail());
		
		return mav;
	}

	public String getIndexPage() {
		return indexPage;
	}

	public void setIndexPage(String indexPage) {
		this.indexPage = indexPage;
	}

	@Override
	public ModelAndView favicon() {
		return new ModelAndView("redirect:resources/images/favicon.ico");
	}

	@Override
	public ModelAndView tab(String page, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("tabs/" + page);
		return mv;
	}
}
